<style scoped>
    .title{
        margin-bottom:10px;
    }
</style>
<template>
    <div class="delivery-list-page">
        <p class="title">
            <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="saveNotice()" :disabled="isClick">保存</el-button>
            <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="commitNotice()" :disabled="isClick">提交</el-button>
            <el-button class="bolder outsideBtnBS" size="mini" type="danger">打印</el-button>
        </p>
        <el-form :inline="true" :model="formInline" class="demo-form-inline" style="margin-bottom:20px;border-bottom:1px solid #ccc;">
            <el-form-item label="预入库单号" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.shipNoticeNo" disabled></el-input>
            </el-form-item>
            <el-form-item label="制单日期" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.createTime" disabled></el-input>
            </el-form-item>
            <el-form-item label="制单人" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.shipNoticceUserName" disabled></el-input>
            </el-form-item>
            <!--<el-form-item label="供应商编码" class="selectMB10">
                <el-input  v-model="formInline.partnerCode" disabled></el-input>
            </el-form-item>
            <el-form-item label="供应商名称" class="selectMB10">
                <el-input  v-model="formInline.partnerName" disabled></el-input>
            </el-form-item>-->
            <el-form-item label="收货地(简称)" class="selectMB10" :label-width="labelWidth100">
                <el-input  v-model="formInline.shortName" disabled></el-input>
            </el-form-item>
            <el-form-item label="详细地址" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.addressDetail" disabled></el-input>
            </el-form-item>
            <!--<el-form-item label="库房地址" class="selectMB10">
                <el-input  v-model="formInline.adressDetailKf" disabled></el-input>
            </el-form-item>-->
            <el-form-item label="联系人" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.linkMan" disabled></el-input>
            </el-form-item>
            <el-form-item label="联系电话" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.linkManMobile" disabled></el-input>
            </el-form-item>
            <el-form-item label="备注" class="selectMB10" :label-width="labelWidth">
                <el-input  v-model="formInline.shipNoticeBak" @change="isClick=false;"></el-input>
            </el-form-item>
        </el-form>
        <el-table :data="createDeliveryData" border style="width: 100%" v-loading.body="loading">
            <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>

            <el-table-column
                    prop="poCode"
                    header-align="center"
                    label="PO"
                    width="100" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="currHopeTime"
                    header-align="center"
                    label="期望到货时间"
                    width="120" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="materialCode"
                    header-align="center"
                    label="备件编码"
                    width="110" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="materialName"
                    header-align="center"
                    label="备件名称"
                    width="110" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="standard"
                    header-align="center"
                    label="规格"
                    width="100" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="materialUnit"
                    header-align="center"
                    label="单位"
                    width="100" show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="shipReqQuantity"
                    header-align="center"
                    label="发货请求数量"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="notReceivedQuantity"
                    header-align="center"
                    label="未发数量"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="currQuantity"
                    header-align="center"
                    label="本次发货数量"
                    width="130">
                <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.currQuantity" placeholder="请输入本次发货数量"  @change="inputCurrQuantity(scope.$index, scope.row)"></el-input>
                </template>
            </el-table-column>
            <el-table-column
                    prop="noticeUnit"
                    header-align="center"
                    label="件（箱）数"
                    width="130">
                <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.noticeUnit" placeholder="送货单位"  @change="inputSpeQuantity(scope.$index, scope.row)"></el-input>
                </template>
            </el-table-column>
        </el-table>

    </div>
</template>
<script>
    export default {
        data(){
            return{
                labelWidth:"80px",
                labelWidth100:"100px",
                // 查询表单
                formInline:{
                    shipNoticeNo:"",
                    createTime:"",
                    shipNoticceUserName:"",
                    shortName:"",
                    addressDetail:"",
                    linkMan:"",
                    linkManMobile:"",
                    shipNoticeBak:""
                },
                createDeliveryData:[],//送货明细列表
                isNum:true,//添加验证送货单位是否是数字
                isCurrNum:true,//验证本次送货数量是否时数字
                shipStatus:"",//保存还是提交
                isTure:true,//本次发货数量必须小于未发数量
                isClick:false,

                loading:false

            }
        },
        //进入页面立即运行此方法
        mounted: function () {
            var _this = this;
            _this.$store.commit('currentActive', 'UpdatePreSheet');
            _this.formInline.shipNoticeNo=_this.$route.query.shipNoticeNo;//传参,通过预入库单号获取需要查看的信息
            _this.getDeliveryInfor();
        },
        methods: {
            //点击修改，一进入页面就获取
            getDeliveryInfor(){
                var _this=this;
                _this.loading=true;
                $.ajax({
                    type: 'GET',
                    url: _this.procurement_service+"shipPreNotice/getNoticeDetail?shipNoticeNo="+_this.formInline.shipNoticeNo,
                    cache: false,
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if(data.success){
                            _this.loading=false;
                            _this.createDeliveryData=data.data.materList;//送货明细表
                            _this.formInline.shipNoticeNo=data.data.shipNoticeNo;//预入库单号
                            _this.formInline.createTime=data.data.createTime;//制单日期
                            _this.formInline.shipNoticceUserName=data.data.shipNoticceUserName;//制单人
                            _this.formInline.shortName=data.data.shortName;//
                            _this.formInline.addressDetail=data.data.addressDetail;//送货地
                            _this.formInline.linkMan=data.data.linkMan;//联系人
                            _this.formInline.linkManMobile=data.data.linkManMobile;//联系电话
                            _this.formInline.shipNoticeBak=data.data.shipNoticeBak;//备注
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }


                    }
                });
            },
            //将填写的本次发货数量加入到对应的行中
            inputCurrQuantity(index,row){
                var _this=this;
                _this.isClick=false;
                if(/^(\-?)(\d+)$/.test(row.currQuantity)){
                    //_this.isCurrNum=true;
                    //验证本次发货数量必须大于发货请求数量
                    if(row.currQuantity<=_this.createDeliveryData[index].notReceivedQuantity){
                       // _this.isTure=true;
                        _this.createDeliveryData[index].currQuantity=row.currQuantity;
                        //console.log(_this.createDeliveryData[index].noticeUnit)
                    }else{
                        //_this.isTure=false;
                        _this.common.errorTip("本次发货数量必须小于未发数量");
                    }

                }else{
                    //_this.isCurrNum=false;
                    _this.common.errorTip("请填入数字");
                }

            },
            //将填写的送货单位加入到对应的行中
            inputSpeQuantity(index,row){
                var _this=this;
                _this.isClick=false;
                // console.log(row.currQuantity);
                //做参数判断，只允许填入数字
                if(/^(\-?)(\d+)$/.test(row.noticeUnit)){
                    //_this.isNum=true;
                    _this.createDeliveryData[index].noticeUnit=row.noticeUnit;
                    //console.log(_this.createDeliveryData[index].noticeUnit)
                }else{
                    //_this.isNum=false;
                    _this.common.errorTip("请填入数字");
                }
            },
            //处理参数
            dealParms(){
                var _this=this;
                var parms=_this.createDeliveryData;
                var newParms=[];
                for(var i=0;i<parms.length;i++){
                    var parmsObj={};
                    parmsObj.poCode=parms[i].poCode;
                    parmsObj.shipRequestNo=parms[i].shipRequestNo;
                    parmsObj.subNoticeNo=parms[i].subNoticeNo;
                    parmsObj.materialCode=parms[i].materialCode;
                    parmsObj.noticeUnit=parms[i].noticeUnit;
                    parmsObj.currQuantity=parms[i].currQuantity;
                    newParms.push(parmsObj);
                }
                return newParms
            },
            //验证本次发货数量
            validSendData(){
                var _this=this;
                var arr=_this.createDeliveryData;
                //console.log(arr)
                for(var i=0;i<arr.length;i++){
                    if(/^(\-?)(\d+)$/.test(arr[i].currQuantity)){
                        //验证本次发货数量必须大于发货请求数量
                        if(arr[i].currQuantity<=_this.createDeliveryData[i].notReceivedQuantity){
                            if(arr[i].currQuantity<=0 || !arr[i].currQuantity){
                                //有一个小于等于0的就返回false
                                return false;
                            }
                        }else{
                            _this.isTure=false;
                            _this.common.errorTip("本次发货数量必须小于未发数量");
                            return false;
                        }
                    }else{
                        _this.isCurrNum=false;
                        _this.common.errorTip("请填入数字");
                        return false;
                    }

                }
                //验证都通过的返回true
                _this.isTure=true;
                _this.isCurrNum=true;
                return true;
            },
            //验证送货单位
            validUnitData(){
                var _this=this;
                var arr=_this.createDeliveryData;
                for(var i=0;i<arr.length;i++){
                    if(/^(\-?)(\d+)$/.test(arr[i].noticeUnit)){
                        if(arr[i].noticeUnit<=0 || !arr[i].noticeUnit){
                            //有一个小于等于0的就返回false
                            return false;
                        }
                    }else{
                        _this.isNum=false;
                        _this.common.errorTip("请填入数字");
                        return false;
                    }

                }
                //验证都通过的返回true
                _this.isNum=true;
                return true;
            },
            //保存
            saveNotice(){
                var _this=this;
                _this.shipStatus="btnSave";
                _this.valiSaveUpdate();
                _this.isClick=true;
            },
            //提交
            commitNotice(){
                var _this=this;
                _this.shipStatus="btnSubmit";
                _this.valiSaveUpdate();
                _this.isClick=true;
            },
            //保存和提交前的验证
            valiSaveUpdate(){
                var _this=this;
                var validSend=_this.validSendData();//验证本次发货数量
                var validUnit=_this.validUnitData();//验证送货单位
                //验证本次发货数量
                if(validSend){
                    if(_this.isCurrNum){
                        //验证本次发货数量必须大于发货请求数量
                        if(_this.isTure){
                            //验证送货单位
                            if(validUnit){
                                if(_this.isNum){
                                    _this.saveUpdateNotice();
                                    //console.log("验证通过")
                                }else{
                                    _this.common.errorTip("送货单位必须是数字");
                                }
                            }else{
                                _this.common.errorTip("请填入送货单位");
                            }
                        }else{
                            _this.common.errorTip("本次发货数量必须小于未发数量");
                        }

                    }else{
                        _this.common.errorTip("本次发货数量必须是数字");
                    }


                }else{
                    _this.common.errorTip("请填入本次发货数量");
                }
            },
            //预入库单的保存和提交方法
            saveUpdateNotice(){
                var _this=this;
                var materList=_this.dealParms();
                var tmp={
                     shipStatus:_this.shipStatus,//btnSave代表保存 ; btnSubmit代表提交
                     shipNoticeNo:_this.formInline.shipNoticeNo,//预入库单号
                     shipNoticeBak:_this.formInline.shipNoticeBak,//备注
                     materList:materList,//备件列表
                }
               // console.log(JSON.stringify(tmp));
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"shipPreNotice/modifyNoticeInfo",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if(data.success){
                            _this.common.successTip("成功");
                            window.location.href="#PreInventorySheet";
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }

                    }
                });
            },
            // //成功
            // successTip(p) {     //成功时的提示信息
            //     this.$notify({
            //         title: '成功',
            //         message: p,
            //         type: 'success'
            //     });
            // },
            // //错误
            // errorTip(p) {     //错误提示
            //     this.$notify.error({
            //         title: '错误',
            //         message: p
            //     });
            // }
        }
    }
</script>


